import Vue from 'vue'
import VueRouter from 'vue-router'
import config from "./router.config";

//解决【$router.push跳转点击两次会报错】问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    // 这个if语句在跳转相同路径的时候，在路径末尾添加新参数（一些随机数字）
    // 用来触发watch
    if (typeof location == "string") {
        let Separator = "&";
        if (location.indexOf("?") == -1) {
            Separator = "?";
        }
        location = location + Separator + "random=" + Math.random();
    } else if (typeof location.path == "string") {
        let Separator = "&";
        if (location.path.indexOf("?") == -1) {
            Separator = "?";
        }
        location.path = location.path + Separator + "random=" + Math.random();
    }

    // 这个语句用来解决报错
    // 调用原来的push函数，并捕获异常
    return originalPush.call(this, location).catch((error) => error);
};

Vue.use(VueRouter);
const router = new VueRouter({
    mode: "hash", //【history、hash】
    routes: config.routeList,
});

export default router;
